<?php 
	

	// Cosas de facebook para logearse y eso
	$app_id = "177160209074782";
	$app_secret = "d29e1578d98135678f119c1cd1c62bf6";
	$canvas_page = "http://apps.facebook.com/thefiubalatexproject";
	
	$auth_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page);
	//&scope=email,read_stream
	
	$signed_request = $_REQUEST["signed_request"];
	
	list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
	
	global $data;
	$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
	
	if (empty($data["user_id"])) {
		echo("<script> top.location.href='" . $auth_url . "'</script>");
	} 
	
	//print_r($data);

	// Cosas de la Database
	conectar_db();
	
	showLatestPosts();
	
	if (isset($_POST["post"]))
	{
		savePost($_POST["post"]);
	} 
		
	function conectar_db()
	{
		global $link;
		$link = mysql_connect("mysql17.000webhost.com", 'a5962093_champ', 'champion0'); 
		mysql_select_db("a5962093_champ", $link); 
	}

	function getNextId()
	{
		global $link;
	}
	
	function savePost($post)
	{
		global $link;
		global $data;
		
		$query = mysql_query("INSERT INTO Content (id, type, author_id, content) VALUES (1, 1, 123, '".$post."' )", $link);
		
		if ( mysql_errno($link) != 0 )
		{
			echo mysql_errno($link);
			echo mysql_error($link);
			return;
		}
	}
	
	function showLatestPosts()
	{
		global $link;
		$query = mysql_query("SELECT * FROM Content", $link);
		
		if ( mysql_errno($link) != 0 )
		{
			echo mysql_errno($link);
			echo mysql_error($link);
			return;
		}
		
		for ($row = 0; $row < mysql_num_rows($query); $row++ )
		{
			echo "<p>".mysql_result($query, $row, "content")."</p>";
		}
		
	}

 ?>
 
<html>
<head>
	
</head>
<body>
	
<form action="index.php" method="post">
  <input type="text" name="post"><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>
